package lumis.doui.source.postloadprocessor;

import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.VariableResolver;

import lumis.doui.DouiContext;
import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.portal.servicecontainer.ServiceContainerRequest;

/**
 * Variable Resolver used during post load processors.
 *
 * @version $Revision: 9601 $ $Date: 2008-07-18 15:38:17 -0300 (Fri, 18 Jul 2008) $
 * @since 4.2.0
 */
public class PostLoadProcessorVariableResolver implements VariableResolver
{
	/**
	 * The source object this variable resolver corresponds to.
	 * @since 4.2.0
	 */
	private final Source<?> source;
	/**
	 * The row object that this resolver corresponds to.
	 * @since 4.2.0
	 */
	private final ISourceData row;

	/**
	 * Creates a new resolver for the given doui context.
	 * @param douiContext the doui context.
	 * @since 4.0.11
	 */
	@SuppressWarnings("unchecked")
	public PostLoadProcessorVariableResolver(ISourceData row, Source source)
	{
		this.row = row;
		this.source = source;
	}

	public Object resolveVariable(String variableName) throws ELException
	{
		if ("source".equals(variableName))
			return source;
		if ("douiContext".equals(variableName))
			return source.getDouiContext();
		else if("request".equals(variableName))
			return source.getDouiContext().getRequest();
		else if("row".equals(variableName))
			return row;
		else
			return ((ServiceContainerRequest)source.getDouiContext().getRequest()).getVariableResolver().resolveVariable(variableName);
	}
}
